/*eslint-disable*/

import router from '@/router';

export const phoneReg = /^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/;

let u = navigator.userAgent;


export function IsPC(){  
  var userAgentInfo = navigator.userAgent;
  var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");  
  var flag = true;  
  for (var v = 0; v < Agents.length; v++) {  
      if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }  
  }  
  return flag;  
}

export let browser = {
  isPC: IsPC(),
  isQQ: u.toLowerCase().match(/QQ/i) == "qq",
  isWeixin: u.toLowerCase().match(/MicroMessenger/i) == "micromessenger",
  isAndroid: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
  isIos: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
}

// 获取url的参数
export function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {return pair[1];}
  }
  return (false);
}

export function stringify(param) {
  let result = [];
  Object.keys(param)
    .forEach(function (key) {
      result.push(key + '=' + encodeURIComponent(param[key]));
    });
  return result.join('&');
}

export function gotoInvite() {
  return setTimeout(() => {
    const code = getQueryVariable('invite_code');
    console.log(code)
    router.push( `/invite?${code ? 'invite_code=' + code : ''}`);
  }, 400);
};

export function dateFormat(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
  let o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}


